package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.Bank;
import com.acme.bankapp.domain.bank.CheckingAccount;
import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.bank.ClientExistsException;
import com.acme.bankapp.domain.bank.Gender;
import com.acme.bankapp.domain.bank.NegativeArgumentValueException;
import com.acme.bankapp.domain.bank.NotEnoughFundsException;
import com.acme.bankapp.domain.bank.OverDraftLimitExceededException;
import com.acme.bankapp.domain.bank.SavingAccount;
import com.acme.bankapp.domain.bank.Bank.DebugListener;
import com.acme.bankapp.domain.bank.Bank.EmailNotificationListener;
import com.acme.bankapp.domain.bank.Bank.PrintClientListener;

public class BankApplication {

	public static void main(String[] args) {
		ClientRegistrationListener[] listeners = clientRegistrationListenersCreator();

		Bank bank = new Bank(listeners);
		fillingBank(bank);

		try {
			BankService.modifyBank(bank);
		} catch (OverDraftLimitExceededException e) {
			System.err.println("OverDraftLimitExceededException: maximum amount to withdraw " + e.getMessage());
		} catch (NotEnoughFundsException e) {
			System.err.println("NotEnoughFundsException: " + e.getAmount());
		} catch (NegativeArgumentValueException e) {
			System.err.println(e.getMessage());
		}

		assert !bank.getClients()[0].equals(bank.getClients()[1]);
		assert bank.getClients()[0] != bank.getClients()[1];
	}

	private static void fillingBank(Bank bank) {
		try {
			Client client = new Client("Alex", Gender.MALE);
			client.addAccount(new CheckingAccount(100, 50));
			BankService.addClient(bank, client);

			client = new Client("Ann", Gender.FEMALE);
			client.addAccount(new SavingAccount(500));
			BankService.addClient(bank, client);

			client = new Client("Alex", Gender.FEMALE);
			client.addAccount(new SavingAccount(100));
			BankService.addClient(bank, client);
		} catch (ClientExistsException e) {
			System.err.println(e.getMessage());
		} catch (NegativeArgumentValueException e) {
			System.err.println(e.getMessage());
		}
	}

	private static ClientRegistrationListener[] clientRegistrationListenersCreator() {
		return new ClientRegistrationListener[] { new DebugListener(), new EmailNotificationListener(), new PrintClientListener() };
	}

}
